package com.example.smartcity1.mainfragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity1.R;
import com.example.smartcity1.adapter.NewsHomeAdapter;
import com.example.smartcity1.bean.BannerBean;
import com.example.smartcity1.bean.NewsBean;
import com.example.smartcity1.bean.NewsTypeBean;
import com.example.smartcity1.network.RetrofitClient;
import com.example.smartcity1.news.NewsPageActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class NewsFragment extends Fragment {

    private static final String TAG = "NewsFragment";
    private List<BannerBean.RowsDTO> mBannerList;
    private List<NewsTypeBean.DataDTO> mNewsTypeList;

    private Banner banner;
    private GridView gridView;
    private ListView listView;

    public NewsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        initView(view);
        getBannerData();
        useBanner();
        setNewsType();
        setNewsList(36);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), " " + mNewsTypeList.get(position).getDictCode(), Toast.LENGTH_SHORT).show();
                setNewsList(mNewsTypeList.get(position).getDictCode());
            }
        });
        listView.setOnItemClickListener((parent, view1, position, id) -> {
            
        });
        return view;
    }

    private void initView(View view) {
        banner = view.findViewById(R.id.banner);
        gridView = view.findViewById(R.id.gridView);
        listView = view.findViewById(R.id.listView);
    }

    //获取轮播图数据
    private void getBannerData() {
        RetrofitClient.appService().getBannerData().enqueue(new Callback<BannerBean>() {
            @Override
            public void onResponse(Call<BannerBean> call, Response<BannerBean> response) {
                BannerBean bannerBean = response.body();
                if (bannerBean != null && bannerBean.getCode() == 200) {
                    mBannerList = bannerBean.getRows();
                    Log.d(TAG, "onResponse: 获取轮播图数据");
                    useBanner();
                }
            }

            @Override
            public void onFailure(Call<BannerBean> call, Throwable t) {
            }
        });
    }

    //使用轮播图
    private void useBanner() {
        banner.setAdapter(new BannerImageAdapter<BannerBean.RowsDTO>(mBannerList) {
            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, BannerBean.RowsDTO rowsDTO, int i, int i1) {
                final String IPP = IP + mBannerList.get(i).getImgUrl();
                Glide.with(getActivity().getApplicationContext())
                        .load(IP + mBannerList.get(i).getImgUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .placeholder(R.drawable.ic_baseline_cloud_download_24)
                        .into(bannerImageHolder.imageView);
                Log.d(TAG, "onResponse: " + "使用轮播图: IP = " + IPP);
            }
        }).addBannerLifecycleObserver(this)//添加生命周期观察者
         .setIndicator(new CircleIndicator(getActivity().getApplicationContext()));
    }

    //设置新闻专栏数据
    private void setNewsType() {
        RetrofitClient.appService().getNewsTypeData().enqueue(new Callback<NewsTypeBean>() {
            @Override
            public void onResponse(Call<NewsTypeBean> call, Response<NewsTypeBean> response) {
                NewsTypeBean newsTypeBean = response.body();
                if (newsTypeBean != null && newsTypeBean.getCode() == 200) {
                    Log.d(TAG, "onResponse: 获取新闻专栏数据");
                    mNewsTypeList = newsTypeBean.getData();
                    List<String> data = new ArrayList<>();
                    for (NewsTypeBean.DataDTO i : mNewsTypeList) {
                        data.add(i.getDictLabel());
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item_news_type, R.id.item_news_type_txt, data);
                    gridView.setAdapter(adapter);

                }
            }

            @Override
            public void onFailure(Call<NewsTypeBean> call, Throwable t) {

            }
        });
    }

    //设置新闻列表数据
    private void setNewsList(int type) {
        RetrofitClient.appService().getNewsHomeData(type).enqueue(new Callback<NewsBean>() {
            @Override
            public void onResponse(Call<NewsBean> call, Response<NewsBean> response) {
                NewsBean newsBean = response.body();
                if (newsBean != null && newsBean.getCode() == 200) {
                    Log.d(TAG, "onResponse: 获取新闻列表数据");
                    List<NewsBean.RowsDTO> mNewsList = newsBean.getRows();
                    NewsHomeAdapter newsHomeAdapter = new NewsHomeAdapter(getActivity().getApplicationContext(), R.layout.item_news_home, mNewsList);
                    listView.setAdapter(newsHomeAdapter);
                    listView.setOnItemClickListener((parent, view, position, id) -> {
                        Gson gson = new Gson();
                        NewsBean.RowsDTO data = mNewsList.get(position);
                        String json = gson.toJson(data);
                        Intent intent = new Intent(getContext(), NewsPageActivity.class);
                        intent.putExtra("news",json);
                        startActivity(intent);
                    });
                }
            }

            @Override
            public void onFailure(Call<NewsBean> call, Throwable t) {

            }
        });
    }
}